/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.example.app.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.json.JSONObject;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.util.Log;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年7月23日 上午9:28:10
 * @Title <B>todo</B>
 * @Description <pre>todo</pre>
 */

public class MobileInfo {
	private static final String TAG="MobileInfo";
	public MobileInfo(){};
	
	/**
	 * @Description <pre>获取手机可用内存和总内存</pre>
	 * @param act
	 * @return String[]{"1-total", "2-avail"}
	 */
	public static String[] getTotalMemory(Context act) {  
        String[] result = {"",""};  //1-total 2-avail  
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); 
        ActivityManager mActivityManager= (ActivityManager)act.getSystemService(Context.ACTIVITY_SERVICE);
        mActivityManager.getMemoryInfo(mi);    
        long mTotalMem = 0;  
        long mAvailMem = mi.availMem;  
        String str1 = "/proc/meminfo";  
        String str2;  
        String[] arrayOfString;  
        try {  
            FileReader localFileReader = new FileReader(str1);  
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);  
            str2 = localBufferedReader.readLine();  
            arrayOfString = str2.split("\\s+");  
            mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;  
            localBufferedReader.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        result[0] = Formatter.formatFileSize(act, mTotalMem);  
        result[1] = Formatter.formatFileSize(act, mAvailMem);  
        Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]);  
        return result;  
    }
	
	/**
	 * @Description <pre>获取手机安装的应用信息（排除系统自带）</pre>
	 * @param ctx
	 * @return String
	 */
	public static String getAllApp(Context ctx) {
		String result = "";
		PackageManager pm=ctx.getPackageManager();
		List<PackageInfo> packages =pm.getInstalledPackages(0);
		for (PackageInfo i : packages) {
			if ((i.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
				result += i.applicationInfo.loadLabel(pm).toString() + ",";
			}
		}
		return result.substring(0, result.length() - 1);
	}
	
	
	/**
	 * @Description <pre>手机CPU信息,和内存信息同理，cpu信息可通过读取/proc/cpuinfo文件来得到，其中第一行为cpu型号，第二行为cpu频率</pre>
	 * @return String[]{"1-cpu型号","2-cpu频率"}
	 */
	public static String[] getCpuInfo() {
		String str1 = "/proc/cpuinfo";
		String str2 = "";
		String[] cpuInfo = { "", "" }; // 1-cpu型号 //2-cpu频率
		String[] arrayOfString;
		try {
			FileReader fr = new FileReader(str1);
			BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
			str2 = localBufferedReader.readLine();
			arrayOfString = str2.split("\\s+");
			for (int i = 2; i < arrayOfString.length; i++) {
				cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
			}
			str2 = localBufferedReader.readLine();
			arrayOfString = str2.split("\\s+");
			cpuInfo[1] += arrayOfString[2];
			localBufferedReader.close();
		} catch (IOException e) {
		}
		Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
		return cpuInfo;
	}
	
	
	/**
	 * @Description <pre>获取手机MAC地址</pre>
	 * @return String
	 */
	public static String getMacAddress(Context ctx) {
		String result = "";
		WifiManager wifiManager = (WifiManager) ctx
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		result = wifiInfo.getMacAddress();
		Log.i(TAG, "macAdd:" + result);
		return result;
	}
	
	/***
	 * @Description <pre>IMEI号，IESI号，手机型号等</pre>
	 * @param ctx
	 * @return String[]{imei,imsi,mtype,numer}
	 */
	public static String[] getTelephonyInfo(Context ctx) {  
        TelephonyManager mTm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = mTm.getDeviceId();  
        String imsi = mTm.getSubscriberId();  
        String mtype =android.os.Build.MODEL; // 手机型号  
        String numer = mTm.getLine1Number(); // 手机号码，有的可得，有的不可得  
        String telephony[]={imei,imsi,mtype,numer};
        return telephony;
	}
	
	public static String getTelephony(Context ctx){
		JSONObject phoneInfo = new JSONObject();
		TelephonyManager tm = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE); 
		try {
			phoneInfo.put("DeviceId(IMEI) ", tm.getDeviceId() ); 
			phoneInfo.put("DeviceSoftwareVersion ", tm.getDeviceSoftwareVersion() ); 
			phoneInfo.put("Line1Number ", tm.getLine1Number() );
			phoneInfo.put("NetworkCountryIso ", tm.getNetworkCountryIso() ); 
			phoneInfo.put("NetworkOperator ", tm.getNetworkOperator() ); 
			phoneInfo.put("NetworkOperatorName ", tm.getNetworkOperatorName() ); 
			phoneInfo.put("NetworkType ", tm.getNetworkType() );
			phoneInfo.put("PhoneType ", tm.getPhoneType() ); 
			phoneInfo.put("SimCountryIso ", tm.getSimCountryIso() ); 
			phoneInfo.put("SimOperator ", tm.getSimOperator() ); 
			phoneInfo.put("SimOperatorName ", tm.getSimOperatorName() ); 
			phoneInfo.put("SimSerialNumber ", tm.getSimSerialNumber() ); 
			phoneInfo.put("SimState ", tm.getSimState() ); 
			phoneInfo.put("SubscriberId(IMSI) ", tm.getSubscriberId() ); 
			phoneInfo.put("VoiceMailNumber ", tm.getVoiceMailNumber() );
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return phoneInfo.toString();
	}
	
	
	
	/**
	 * @Description <pre>获取手机屏幕宽高</pre>
	 * @param ctx
	 * @return float[]{widthPixels,heightPixels,density};
	 */
	public static float[] getDisplayMetrics(Context ctx){  
		DisplayMetrics dm=ctx.getResources().getDisplayMetrics();
		float[]wh={dm.widthPixels,dm.heightPixels,dm.density,dm.scaledDensity};
		return wh;
		
        //这种方式在service中无法使用，  
		// DisplayMetrics dm = new DisplayMetrics();
		// ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
		// int width = dm.widthPixels; //宽
		// int height = dm.heightPixels; //高
   
		// //在service中也能得到高和宽
		// WindowManager mWindowManager = (WindowManager)
		// ctx.getSystemService(Context.WINDOW_SERVICE);
		// width = mWindowManager.getDefaultDisplay().getWidth();
		// height = mWindowManager.getDefaultDisplay().getHeight();  
    }
	
	
	public static String getSoftInfo() {
		JSONObject phoneInfo = new JSONObject();
		try {
			phoneInfo.put("Product: ", android.os.Build.PRODUCT);
			phoneInfo.put("CPU_ABI: ", android.os.Build.CPU_ABI);
			phoneInfo.put("TAGS: ", android.os.Build.TAGS);
			phoneInfo.put("VERSION_CODES.BASE: ",android.os.Build.VERSION_CODES.BASE);
			phoneInfo.put("MODEL: ", android.os.Build.MODEL);
			phoneInfo.put("SDK: ", android.os.Build.VERSION.SDK);
			phoneInfo.put("VERSION.RELEASE: ", android.os.Build.VERSION.RELEASE);
			phoneInfo.put("DEVICE: ", android.os.Build.DEVICE);
			phoneInfo.put("DISPLAY: ", android.os.Build.DISPLAY);
			phoneInfo.put("BRAND: ", android.os.Build.BRAND);
			phoneInfo.put("BOARD: ", android.os.Build.BOARD);
			phoneInfo.put("FINGERPRINT: ", android.os.Build.FINGERPRINT);
			phoneInfo.put("ID: ", android.os.Build.ID);
			phoneInfo.put("MANUFACTURER: ", android.os.Build.MANUFACTURER);
			phoneInfo.put("USER: ", android.os.Build.USER);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return phoneInfo.toString();
	}
	
	
	
	
	
}
